package vn.vfriends.vbox.service.impl;

import java.util.List;
import java.util.concurrent.Future;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import vn.vfriends.vbox.model.Storage;
import vn.vfriends.vbox.model.File;
import vn.vfriends.vbox.rest.ApiConfig;
import vn.vfriends.vbox.rest.Authenticator;
import vn.vfriends.vbox.service.StorageService;

/**
 *
 * @author tuancn
 */
@Stateless
public class StorageServiceImpl implements StorageService {

    private Client client;
    
    @Inject @ApiConfig("API_URL")
    private String apiUrl;
    
    @Inject @ApiConfig("API_USER")
    private String apiUser;
    
    @Inject @ApiConfig("API_PASSWORD")
    private String apiPassword;
    
    @PostConstruct
    private void init() {
        client = ClientBuilder.newClient().register(new Authenticator(apiUser, apiPassword));
    }
    
    @Override
    public Future<Storage> getStorage(Integer id) {
        WebTarget target = client.target(apiUrl + "/storages").path(id.toString());
        return target.request().async().get(Storage.class);
    }

    @Override
    public Future<List<File>> getFiles(Integer accountId, String fileId) {
        WebTarget target = client.target(apiUrl + "/storages/files")
                .queryParam("accountId", accountId).queryParam("fileId", fileId);
        return target.request().async().get(new GenericType<List<File>>() {});
    }
    
}
